package com.wangijun.yuan.web.api;


import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.wangijun.yuan.web.base.CommonResult;
import com.wangijun.yuan.core.base.YesNoEnum;
import com.wangijun.yuan.core.domain.Category;
import com.wangijun.yuan.core.domain.TagInfo;
import com.wangijun.yuan.core.domain.TopicInfo;
import com.wangijun.yuan.core.model.Group;
import com.wangijun.yuan.core.model.IdParam;
import com.wangijun.yuan.web.model.dto.ArticleParam;
import com.wangijun.yuan.web.model.vo.EditArticleVO;
import com.wangijun.yuan.web.model.vo.SelectVO;
import com.wangijun.yuan.web.service.ArticleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "5-文章操作")
@RestController
@RequiredArgsConstructor
@RequestMapping("/article-operate")
public class ArticleOperateController {
    private final ArticleService articleService;

    @Schema(description = "添加博客页面初始数据")
    public record AddArticleInit(
            @Schema(description = "分类数据")
            List<SelectVO> categoryList,
            @Schema(description = "标签数据")
            List<SelectVO> tagList,
            @Schema(description = "话题数据")
            List<SelectVO> topicList
    ) {
    }

    @Operation(summary = "添加页面参数", description = "添加页面参数加载")
    @GetMapping("/init")
    CommonResult<AddArticleInit> init() {
        List<Category> categoryList = Db.list(Category.class);
        List<TagInfo> tagInfoList = Db.list(TagInfo.class);
        List<TopicInfo> topicInfos = Db.list(TopicInfo.class);
        return CommonResult.data(new AddArticleInit(
                categoryList.stream().map(i -> new SelectVO(i.getName(), i.getId())).toList(),
                tagInfoList.stream().map(i -> new SelectVO(i.getName(), i.getId())).toList(),
                topicInfos.stream().map(i -> new SelectVO(i.getName(), i.getId())).toList()

        ));
    }

    @Operation(summary = "编辑文章参数", description = "编辑文章参数加载")
    @PostMapping("/edit-data")
    CommonResult<EditArticleVO> editData(@RequestBody @Validated IdParam param) {
        return CommonResult.data(articleService.editData(param.getId()));
    }

    @Operation(summary = "发布文章", description = "发布文章")
    @PostMapping("/push")
    CommonResult<Long> push(@RequestBody @Validated({Group.Save.class}) ArticleParam param) {
        param.setStatus(YesNoEnum.YES);
        return CommonResult.data(articleService.push(param));
    }
    @Operation(summary = "编辑文章", description = "编辑文章")
    @PostMapping("/edit")
    CommonResult<Long> edit(@RequestBody @Validated({Group.Save.class}) ArticleParam param) {
        return CommonResult.data(articleService.edit(param));
    }

    @Operation(summary = "保存草稿", description = "保存草稿")
    @PostMapping("/to-draft")
    CommonResult<Long> toDraft(@RequestBody @Validated({Group.Save.class}) ArticleParam param) {
        param.setStatus(YesNoEnum.NO);
        return CommonResult.data(articleService.toDraft(param));
    }

    @Operation(summary = "删除文章", description = "删除文章")
    @GetMapping("/del")
    CommonResult<Boolean> delArticle() {

        return CommonResult.data(false);
    }
}
